package com.kurumi.controller;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kurumi.pojo.MedicalRecordPageIndex2016;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.service.MedicalRecordCheckService;
import com.kurumi.service.MedicalRecordPageIndexService;
import com.kurumi.service.MedicalRecordPageIndexWjtCheckService;
import com.kurumi.service.MedicalRecordService;
import com.kurumi.service.PageIndexRefService;
import com.kurumi.util.DateUtil;
import com.kurumi.util.ExcelUtils;
import com.kurumi.util.NumberUtils;
import com.kurumi.util.ObjectUtils;

@Controller
@RequestMapping("/excel")
public class ExcelController {

	@Autowired
	private MedicalRecordService medicalRecordService;
	
	@Autowired
	private MedicalRecordCheckService medicalRecordCheckService;
	
	@Autowired
	private MedicalRecordPageIndexService medicalRecordPageIndexService;
	
	@Autowired
	private MedicalRecordPageIndexWjtCheckService pageIndexWjtCheckService; 
	
	@Autowired
	private PageIndexRefService pageIndexRefService;
	
	@GetMapping("/pigeonhole_rate")
	public void pigeonholedRateToExcel(MedicalRecordQuery params,HttpServletResponse response) {
		List<String> headers = new ArrayList<String>();
		headers.add("科室名称");
		headers.add("应归病案数");
		headers.add("已归病案数");
		headers.add("归档率");
		headers.add("归档数(2日)");
		headers.add("归档率(2日)");
		headers.add("归档数(3日)");
		headers.add("归档率(3日)");
		headers.add("归档数(7日)");
		headers.add("归档率(7日)");
		OutputStream out = null;
		List<String> keys = new ArrayList<String>();
		keys.add("out_dept_name");
		keys.add("medical_record_count");
		keys.add("medical_record_pigeonhold_count");
		keys.add("medical_record_pigeonhold_rate");
		keys.add("two_dys_pigeonhole_count");
		keys.add("two_dys_pigeonhole_rate");
		keys.add("three_dys_pigeonhole_count");
		keys.add("three_dys_pigeonhole_rate");
		keys.add("seven_dys_pigeonhole_count");
		keys.add("seven_dys_pigeonhole_rate");
		
		try{
			List<Map<String,Object>> medicalRecordRates = new ArrayList<Map<String,Object>>();
			
			if(!params.queryUnEncodingEmpty()){
				medicalRecordRates = medicalRecordService.getMedicalRecordPigeonholedRate(params);
			}
			
			response.setContentType("applicationnd.ms-excel");
			String title= "签收统计"+DateUtil.dateFormat(new Date());
			StringBuilder downLoadFileName = new StringBuilder();
			downLoadFileName = new StringBuilder(
					"attachment;filename=");
			downLoadFileName.append(title)
					.append(".xls");
			/*downLoadFileName.append(title).append(DateUtil.dateFormat(new Date()))
			.append(".dbf");*/
			String encodeStr = new String(downLoadFileName.toString().getBytes(
					"GB2312"), "ISO_8859_1");
			response.addHeader("Content-Disposition", encodeStr);
			out = response.getOutputStream();
			Integer totalCount = 0;
			Integer pigeonholdTotalCount = 0;
			Integer twoDysPigeonholeTotalCount = 0;
			Integer threeDysPigeonholeTotalCount = 0;
			Integer sevenDysPigeonholeTotalCount = 0;
			List<Map<String, String>> records = new ArrayList<Map<String,String>>();
			for (Map<String, Object> medicalRecordRate : medicalRecordRates) {
				
				Map<String, String> record = new HashMap<String, String>();
				record.put("out_dept_name",(String)medicalRecordRate.get("out_dept_name"));
				Integer medicalRecordCount = ObjectUtils.getIntegerValue(medicalRecordRate.get("medical_record_count"));
				if(medicalRecordCount != null){
					totalCount += medicalRecordCount;
					record.put("medical_record_count",medicalRecordCount+"");
				}else{
					record.put("medical_record_count","0");
				}
				
				Integer medicalRecordPigeonholdCount = ObjectUtils.getIntegerValue(medicalRecordRate.get("medical_record_pigeonhold_count"));
				if(medicalRecordPigeonholdCount != null){
					pigeonholdTotalCount += medicalRecordPigeonholdCount;
					record.put("medical_record_pigeonhold_count",medicalRecordPigeonholdCount+"");
				}else{
					record.put("medical_record_pigeonhold_count","0");
				}
				
				BigDecimal medicalRecordPigeonholdRate = ObjectUtils.getBigDecimalValue(medicalRecordRate.get("medical_record_pigeonhold_rate"));
				if(medicalRecordPigeonholdRate != null){
					record.put("medical_record_pigeonhold_rate",NumberUtils.getString(medicalRecordPigeonholdRate, 2)+"%");
				}else{
					record.put("medical_record_pigeonhold_rate","0%");
				}
				
				Integer twoDysPigeonholeCount = ObjectUtils.getIntegerValue(medicalRecordRate.get("two_dys_pigeonhole_count"));
				if(twoDysPigeonholeCount != null){
					twoDysPigeonholeTotalCount += twoDysPigeonholeCount;
					record.put("two_dys_pigeonhole_count",twoDysPigeonholeCount+"");
				}else{
					record.put("two_dys_pigeonhole_count","0");
				}
				BigDecimal twoDysPigeonholeRate = ObjectUtils.getBigDecimalValue(medicalRecordRate.get("two_dys_pigeonhole_rate"));
				if(twoDysPigeonholeRate != null){
					record.put("two_dys_pigeonhole_rate",NumberUtils.getString(twoDysPigeonholeRate, 2)+"%");
				}else{
					record.put("two_dys_pigeonhole_rate","0%");
				}
				Integer threeDysPigeonholeCount = ObjectUtils.getIntegerValue(medicalRecordRate.get("three_dys_pigeonhole_count"));
				if(threeDysPigeonholeCount != null){
					threeDysPigeonholeTotalCount += threeDysPigeonholeCount;
					record.put("three_dys_pigeonhole_count",threeDysPigeonholeCount+"");
				}else{
					record.put("three_dys_pigeonhole_count","0");
				}
				BigDecimal threeDysPigeonholeRate = ObjectUtils.getBigDecimalValue(medicalRecordRate.get("three_dys_pigeonhole_rate"));
				if(threeDysPigeonholeRate != null){
					record.put("three_dys_pigeonhole_rate",NumberUtils.getString(threeDysPigeonholeRate, 2)+"%");
				}else{
					record.put("three_dys_pigeonhole_rate","0%");
				}
				
				Integer sevenDysPigeonholeCount = ObjectUtils.getIntegerValue(medicalRecordRate.get("seven_dys_pigeonhole_count"));
				if(sevenDysPigeonholeCount != null){
					sevenDysPigeonholeTotalCount += sevenDysPigeonholeCount;
					record.put("seven_dys_pigeonhole_count",sevenDysPigeonholeCount+"");
				}else{
					record.put("seven_dys_pigeonhole_count","0");
				}
				
				BigDecimal sevenDysPigeonholeRate = ObjectUtils.getBigDecimalValue(medicalRecordRate.get("seven_dys_pigeonhole_rate"));
				if(sevenDysPigeonholeRate != null){
					record.put("seven_dys_pigeonhole_rate",NumberUtils.getString(sevenDysPigeonholeRate, 2)+"%");
				}else{
					record.put("seven_dys_pigeonhole_rate","0%");
				}
				
				records.add(record);
			}
			if(params.getOutHospitalDeptCode() == null){
				Map<String, String> totalRecord = new HashMap<String, String>();
				totalRecord.put("out_dept_name","合计（全院）");
				totalRecord.put("medical_record_count",totalCount+"");
				totalRecord.put("medical_record_pigeonhold_count",pigeonholdTotalCount+"");
				totalRecord.put("two_dys_pigeonhole_count",twoDysPigeonholeTotalCount+"");
				totalRecord.put("three_dys_pigeonhole_count",threeDysPigeonholeTotalCount+"");
				totalRecord.put("seven_dys_pigeonhole_count",sevenDysPigeonholeTotalCount+"");
				if(totalCount != 0){
					totalRecord.put("medical_record_pigeonhold_rate",NumberUtils.getPrecent(new BigDecimal(""+pigeonholdTotalCount),new BigDecimal(""+totalCount))+"%");
					totalRecord.put("two_dys_pigeonhole_rate",NumberUtils.getPrecent(new BigDecimal(""+twoDysPigeonholeTotalCount),new BigDecimal(""+totalCount))+"%");
					totalRecord.put("three_dys_pigeonhole_rate",NumberUtils.getPrecent(new BigDecimal(""+threeDysPigeonholeTotalCount),new BigDecimal(""+totalCount))+"%");
					totalRecord.put("seven_dys_pigeonhole_rate",NumberUtils.getPrecent(new BigDecimal(""+sevenDysPigeonholeTotalCount),new BigDecimal(""+totalCount))+"%");
				}else{
					totalRecord.put("medical_record_pigeonhold_rate","0%");
					totalRecord.put("two_dys_pigeonhole_rate","0%");
					totalRecord.put("three_dys_pigeonhole_rate","0%");
					totalRecord.put("seven_dys_pigeonhole_rate","0%");
				}
				
				records.add(totalRecord);
			}
			
			ExcelUtils.exportExcel(out,headers, keys, records);
			
			
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(out != null){
				try {
					out.flush();
				} catch (Exception e2) {
					// TODO: handle exception
				}
				try {
					out.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
		}
		
	}
	
	
	@GetMapping("/quality_control_score_rate")
	public void qualityControlScoreRateToExcel(MedicalRecordQuery params,HttpServletResponse response) {
		List<String> headers = new ArrayList<String>();
		headers.add("科室名称");
		headers.add("应质控数");
		headers.add("已质控数");
		headers.add("质控率");
		headers.add("病案数(甲级)");
		headers.add("比率(甲级)");
		headers.add("病案数(乙级)");
		headers.add("比率(乙级)");
		headers.add("病案数(丙级)");
		headers.add("比率(丙级)");
		OutputStream out = null;
		List<String> keys = new ArrayList<String>();
		keys.add("out_dept_name");
		keys.add("medical_record_count");
		keys.add("quality_control_count");
		keys.add("quality_control_rate");
		keys.add("quality_control_a_count");
		keys.add("quality_control_a_rate");
		keys.add("quality_control_b_count");
		keys.add("quality_control_b_rate");
		keys.add("quality_control_b_count");
		keys.add("quality_control_b_rate");
		
		try{
			List<Map<String,Object>> qualityControlScoreRates = new ArrayList<Map<String,Object>>();
			if(!params.queryUnPigeonholeEmpty()){
				qualityControlScoreRates = medicalRecordService.getQualityControlScoreRate(params);
			}
			
			response.setContentType("applicationnd.ms-excel");
			String title= "质控评分统计"+DateUtil.dateFormat(new Date());
			StringBuilder downLoadFileName = new StringBuilder();
			downLoadFileName = new StringBuilder(
					"attachment;filename=");
			downLoadFileName.append(title)
					.append(".xls");
			/*downLoadFileName.append(title).append(DateUtil.dateFormat(new Date()))
			.append(".dbf");*/
			String encodeStr = new String(downLoadFileName.toString().getBytes(
					"GB2312"), "ISO_8859_1");
			response.addHeader("Content-Disposition", encodeStr);
			out = response.getOutputStream();
			Integer totalCount = 0;
			Integer qualityControlTotalCount = 0;
			Integer qualityControlATotalCount = 0;
			Integer qualityControlBTotalCount = 0;
			Integer qualityControlCCountTotalCount = 0;
			List<Map<String, String>> records = new ArrayList<Map<String,String>>();
			for (Map<String, Object> qualityControlScoreRate : qualityControlScoreRates) {
				
				Map<String, String> record = new HashMap<String, String>();
				record.put("out_dept_name",(String)qualityControlScoreRate.get("out_dept_name"));
				Integer medicalRecordCount = ObjectUtils.getIntegerValue(qualityControlScoreRate.get("total_count"));
				if(medicalRecordCount != null){
					totalCount += medicalRecordCount;
					record.put("medical_record_count",medicalRecordCount+"");
				}else{
					record.put("medical_record_count","0");
				}
				
				Integer qualityControlCount = ObjectUtils.getIntegerValue(qualityControlScoreRate.get("quality_control_count"));
				if(qualityControlCount != null){
					qualityControlTotalCount += qualityControlCount;
					record.put("quality_control_count",qualityControlCount+"");
				}else{
					record.put("quality_control_count","0");
				}
				
				BigDecimal qualityControlRate = ObjectUtils.getBigDecimalValue(qualityControlScoreRate.get("quality_control_rate"));
				if(qualityControlRate != null){
					record.put("quality_control_rate",NumberUtils.getString(qualityControlRate, 2)+"%");
				}else{
					record.put("quality_control_rate","0%");
				}
				
				Integer qualityControlACount = ObjectUtils.getIntegerValue(qualityControlScoreRate.get("quality_control_a_count"));
				if(qualityControlACount != null){
					qualityControlATotalCount += qualityControlACount;
					record.put("quality_control_a_count",qualityControlACount+"");
				}else{
					record.put("quality_control_a_count","0");
				}
				BigDecimal qualityControlARate = ObjectUtils.getBigDecimalValue(qualityControlScoreRate.get("quality_control_a_rate"));
				if(qualityControlARate != null){
					record.put("quality_control_a_rate",NumberUtils.getString(qualityControlARate, 2)+"%");
				}else{
					record.put("quality_control_a_rate","0%");
				}
				Integer qualityControlBCount = ObjectUtils.getIntegerValue(qualityControlScoreRate.get("quality_control_b_count"));
				if(qualityControlBCount != null){
					qualityControlBTotalCount += qualityControlBCount;
					record.put("quality_control_b_count",qualityControlBCount+"");
				}else{
					record.put("quality_control_b_count","0");
				}
				BigDecimal qualityControlBRate = ObjectUtils.getBigDecimalValue(qualityControlScoreRate.get("quality_control_b_rate"));
				if(qualityControlBRate != null){
					record.put("quality_control_b_rate",NumberUtils.getString(qualityControlBRate, 2)+"%");
				}else{
					record.put("quality_control_b_rate","0%");
				}
				
				Integer qualityControlCCount = ObjectUtils.getIntegerValue(qualityControlScoreRate.get("quality_control_c_count"));
				if(qualityControlCCount != null){
					qualityControlCCountTotalCount += qualityControlCCount;
					record.put("quality_control_c_count",qualityControlCCount+"");
				}else{
					record.put("quality_control_c_count","0");
				}
				
				BigDecimal qualityControlCRate = ObjectUtils.getBigDecimalValue(qualityControlScoreRate.get("quality_control_c_rate"));
				if(qualityControlCRate != null){
					record.put("quality_control_c_rate",NumberUtils.getString(qualityControlCRate, 2)+"%");
				}else{
					record.put("quality_control_c_rate","0%");
				}
				
				records.add(record);
			}
			if(params.getOutHospitalDeptCode() == null){
				Map<String, String> totalRecord = new HashMap<String, String>();
				totalRecord.put("out_dept_name","合计（全院）");
				totalRecord.put("medical_record_count",totalCount+"");
				totalRecord.put("quality_control_count",qualityControlTotalCount+"");
				
				totalRecord.put("quality_control_a_count",qualityControlATotalCount+"");
				totalRecord.put("quality_control_b_count",qualityControlBTotalCount+"");
				totalRecord.put("quality_control_c_count",qualityControlCCountTotalCount+"");
				if(totalCount != 0){
					totalRecord.put("quality_control_rate",NumberUtils.getPrecent(new BigDecimal(""+qualityControlTotalCount),new BigDecimal(""+totalCount))+"%");
					totalRecord.put("quality_control_a_rate",NumberUtils.getPrecent(new BigDecimal(""+qualityControlATotalCount),new BigDecimal(""+totalCount))+"%");
					totalRecord.put("quality_control_b_rate",NumberUtils.getPrecent(new BigDecimal(""+qualityControlBTotalCount),new BigDecimal(""+totalCount))+"%");
					totalRecord.put("quality_control_c_rate",NumberUtils.getPrecent(new BigDecimal(""+qualityControlCCountTotalCount),new BigDecimal(""+totalCount))+"%");
				}else{
					totalRecord.put("quality_control_rate","0%");
					totalRecord.put("quality_control_a_rate","0%");
					totalRecord.put("quality_control_b_rate","0%");
					totalRecord.put("quality_control_c_rate","0%");
				}
				
				records.add(totalRecord);
			}
			
			ExcelUtils.exportExcel(out,headers, keys, records);
			
			
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(out != null){
				try {
					out.flush();
				} catch (Exception e2) {
					// TODO: handle exception
				}
				try {
					out.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
		}
		
	}
	
	
	@GetMapping("/page_index_check")
	public void pageIndexCheckToExcel(MedicalRecordQuery params,HttpServletResponse response) {
		List<String> headers = new ArrayList<String>();
		headers.add("住院号");
		headers.add("病案号");
		headers.add("住院次数");
		headers.add("姓名");
		//headers.add("出院科室");
		headers.add("出院日期");
		//headers.add("离院方式");
		headers.add("审核结果");
		OutputStream out = null;
		List<String> keys = new ArrayList<String>();
		keys.add("only_id");
		keys.add("mr_id");
		keys.add("visit_number");
		keys.add("patient_name");
		//keys.add("out_dept_name");
		keys.add("out_hospital_date");
		//keys.add("out_hospital_type_name");
		keys.add("error_desc");
		
		try{
			List<Map<String,Object>> checkMedicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.queryUnEncodingEmpty()){
				Map<String,Map<String,String>> pageIndexDectRefs = pageIndexRefService.getPageIndexWjtDectRefs();
				Map<String, Map<String, Object>> gbtDectRefs = pageIndexRefService.getGbtDectRefs();
				//List<MedicalRecordPageIndexInterface> pageIndexs = medicalRecordPageIndexService.getMedicalRecordPageIndexInterface(params);
				List<MedicalRecordPageIndex2016> pageIndexs = medicalRecordPageIndexService.getMedicalRecordPageIndex2016(params);
				checkMedicalRecords = pageIndexWjtCheckService.pageIndexCheckWjt(pageIndexs, pageIndexDectRefs, gbtDectRefs);
				//checkMedicalRecords = medicalRecordCheckService.pageIndexCheck(params);
			}
			response.setContentType("applicationnd.ms-excel");
			String title= "首页质控"+DateUtil.dateFormat(new Date());
			StringBuilder downLoadFileName = new StringBuilder();
			downLoadFileName = new StringBuilder(
					"attachment;filename=");
			downLoadFileName.append(title)
					.append(".xls");
			/*downLoadFileName.append(title).append(DateUtil.dateFormat(new Date()))
			.append(".dbf");*/
			String encodeStr = new String(downLoadFileName.toString().getBytes(
					"GB2312"), "ISO_8859_1");
			response.addHeader("Content-Disposition", encodeStr);
			out = response.getOutputStream();
			List<Map<String, String>> records = new ArrayList<Map<String,String>>();
			for (Map<String, Object> checkMedicalRecord : checkMedicalRecords) {
				
				Map<String, String> record = new HashMap<String, String>();
				record.put("only_id",ObjectUtils.getStringValue(checkMedicalRecord.get("only_id")));
				record.put("mr_id",ObjectUtils.getStringValue(checkMedicalRecord.get("mr_id")));
				record.put("visit_number",ObjectUtils.getStringValue(checkMedicalRecord.get("visit_number")));
				record.put("patient_name",ObjectUtils.getStringValue(checkMedicalRecord.get("patient_name")));
				
				//record.put("out_dept_name",ObjectUtils.getStringValue(checkMedicalRecord.get("out_dept_name")));
				record.put("out_hospital_date",ObjectUtils.getStringValue(checkMedicalRecord.get("in_hospital_date_format")));
				//record.put("out_hospital_type_name",ObjectUtils.getStringValue(checkMedicalRecord.get("out_hospital_type_name")));
				record.put("error_desc",ObjectUtils.getStringValue(checkMedicalRecord.get("error_desc")));
				records.add(record);
			}
			
			ExcelUtils.exportExcel(out,headers, keys, records);
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(out != null){
				try {
					out.flush();
				} catch (Exception e2) {
					// TODO: handle exception
				}
				try {
					out.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
		}
		
	}
	
	
}
